<template>
  <div>
    <!-- 添加 -->
    <div>
      <button class="btn" @click="dialogFormVisible = true">添加分组</button>
    </div>
    <div class="list_box">
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column
          prop="name"
          label="标签名"
          width="250"
        ></el-table-column>
        <el-table-column
          prop="number"
          label="会员数"
          width="250"
        ></el-table-column>
        <el-table-column
          prop="describe"
          label="标签描述"
          width="250"
        ></el-table-column>
        <el-table-column prop="zip" label="邮编" width="250">
          <template slot-scope="scope">
            <span @click="dialogFormVisible = true">添加会员</span>
            <el-button
              @click="handleClick(scope.row)"
              type="text"
              size="medium"
            >
              删除会员
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog title="添加标签组" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标签名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="标签描述" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="textarea2"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addlist">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { zqhdivide,zqhdivideadd,zqhdividedelete } from '../../api/index'
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
      },
      formLabelWidth: '120px',
      textarea2: '',
    }
  },
  created() {
    this.list()
  },
  methods: {
    // 获取数据
    list() {
      zqhdivide().then((res) => {
        console.log(res)
        if (res.code == 200) {
          this.tableData = res.data
        }
      })
    },
    // 删除数据
    handleClick(row) {
      console.log(row)
      zqhdividedelete({id:row.id}).then((res)=>{
        console.log(res)
        if(res.code==200){
          this.$notify({
              title: '删除成功',
              message: '数据已删除成功',
              type: 'success',
            })
          this.list()
        }
      })
    },
    // 添加数据
    addlist(){
      if(this.textarea2!=''&&this.form.name!=''){
        console.log(this.textarea2,this.form.name)
      zqhdivideadd({name:this.form.name,number:20,describe:this.textarea2}).then((res)=>{
        console.log(res)
        if(res.code==200){
          this.$notify({
              title: '成功',
              message: '恭喜你添加成功',
              type: 'success',
            })
          this.list()
          this.textarea2=''
          this.form.name=''
        }
      })
      this.dialogFormVisible = false
      }else{
        this.$notify({
          title: '警告',
          message: '请输入要添加的等级',
          type: 'warning',
        })
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.btn {
  width: 110px;
  height: 30px;
  border-radius: 5px;
  background-color: white;
  box-shadow: 1px 1px 2px gainsboro;
  border: 1px solid gainsboro;
  margin: 30px 0 0 100px;
}
.list_box {
  width: 1001px;
  margin: 50px auto;
}
</style>
